package avicit.bdp.dms.tdm.service.adapter;

import avicit.bdp.common.database.ColumnMeta;
import avicit.bdp.common.service.dto.BdpColumnDTO;
import avicit.bdp.common.service.service.DataSourceService;
import avicit.bdp.common.utils.CheckParaUtils;
import avicit.bdp.core.poi.util.csv.ExcelUtils;
import avicit.bdp.core.poi.util.csv.style.ExcelExportTitleStyle;
import avicit.bdp.core.util.json.JSONUtils;
import avicit.bdp.dms.das.service.BdpDasColumnService;
import avicit.bdp.dms.tdm.dto.BdpAssetDTO;
import avicit.bdp.dms.tdm.dto.BdpAssetMountModelDTO;
import avicit.bdp.dms.tdm.service.BdpAssetMountModelService;
import avicit.platform6.commons.utils.PojoUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.alibaba.fastjson2.JSONArray;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Component
public class ModelAdapter {

  private static final Logger logger = LoggerFactory.getLogger(ModelAdapter.class);

  @Autowired
  private BdpAssetMountModelService assetMountModelService;
  @Autowired
  private DataSourceService dataSourceService;
  @Autowired
  private BdpDasColumnService columnService;

  /**
   * 查询模型字段
   * @param assetId
   * @return
   * @throws Exception
   */
  public List<ColumnMeta> getModelColumnList(String assetId) throws Exception {
    BdpAssetMountModelDTO model = assetMountModelService.queryByAssetId(assetId);
    CheckParaUtils.checkDbObjectValidity(model, logger, String.format("没有查到数据，请确保assetId:%s的模型是否存在", assetId));

    List<ColumnMeta> list = new ArrayList<>();
    String modelType = model.getModelType();
    if("1".equals(modelType)){
      JSONArray array = JSONUtils.parseArray(model.getColumnInfo());
      list = array.toJavaList(ColumnMeta.class);
    }else if("2".equals(modelType)){
      BdpColumnDTO columnDTO = new BdpColumnDTO();
      columnDTO.setTableId(model.getMetadataId());
      List<BdpColumnDTO> columnList = columnService.selectList(columnDTO);
      if (CollectionUtils.isNotEmpty(columnList)) {
        for(BdpColumnDTO bdpColumn : columnList){
          ColumnMeta columnMeta = new ColumnMeta();
          PojoUtil.copyProperties(columnMeta, bdpColumn, true);
          list.add(columnMeta);
        }
      }
    }else if("3".equals(modelType)){
      list = dataSourceService.getColumnList(model.getDatasourceId(), model.getTableName());
    }

    return list;
  }

  /**
   * 模型下载
   * @param assetId
   * @param response
   * @throws Exception
   */
  public void downloadModel(String assetId, HttpServletResponse response) throws Exception {
    List<ColumnMeta> columnList = getModelColumnList(assetId);
    if (CollectionUtils.isNotEmpty(columnList)) {
      ExcelUtils excelUtils = new ExcelUtils();

      // 设置导出参数
      ExportParams params = new ExportParams();
      // 设置excel类型，XSSF代表xlsx，HSSF代表xls
      params.setType(ExcelType.XSSF);
      params.setStyle(ExcelExportTitleStyle.class);
      excelUtils.baseExport(columnList, ColumnMeta.class, "模型数据", params, response);
    }
  }

}
